﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using JetBrains.Annotations;

namespace WikiDoc.Lines
{
    /// <summary>
    /// 
    /// </summary>
    class BulletListLine : ListLine
    {
        protected BulletListLine(string line, int level, int level2, string content)
            : base(line, LineTypes.BulletList, level, level2, content)
        {
        }

        /// <summary>
        /// 列表行定义：[至少一个空格] [*-×—] [至少一个空格] 内容
        /// </summary>
        public static readonly Regex Pattern = new Regex(
            @"\A (\s+)  ([*×\-—]\s+)  (.*)",
            RegexOptions.Compiled |
            RegexOptions.CultureInvariant |
            RegexOptions.Singleline |
            RegexOptions.IgnorePatternWhitespace);

        /// <summary>
        /// Tries the parse.
        /// </summary>
        /// <param name="line">The line.</param>
        /// <returns></returns>
        [CanBeNull]
        public static BulletListLine TryParse([CanBeNull] string line)
        {
            if (line == null) return null;

            var match = Pattern.Match(line);

            return match.Success
                       ? new BulletListLine(line,
                                      match.Groups[1].Length,
                                      match.Groups[1].Length + match.Groups[2].Length,
                                      match.Groups[3].Value)
                       : null;
        }
    }
}
